package com.example.newinfinity

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.example.newinfinity.MyViewModel.UserViewModel
import com.example.newinfinity.UserRoom.User
import com.example.newinfinity.databinding.FragmentHomeBinding
import com.example.newinfinity.databinding.FragmentRegisterBinding

class RegisterFragment : BaseFragment<FragmentRegisterBinding>() {
    private val userViewModel: UserViewModel by activityViewModels()
    //用于保存注册时用户在editText中输入的用户信息
    private var userName = ""
    private var userPassword = ""

    override fun initViewBinding(): FragmentRegisterBinding {
        return FragmentRegisterBinding.inflate(layoutInflater)
    }

    //在该方法中可编写onCreateView时期需要编写的内容
    override fun initView() {
        super.initView()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //注册button 点击按钮注册完毕 重新跳转到登录界面进行登录
        registerToLoginFragment()
    }

    //TextInputEditText（继承自EditText）中，text属性返回的是一个非空Editable对象
    //即使用户没有输入任何内容，mBinding.userName.text也不会为 null，而是指向一个 空字符串的Editable。
    //因此，直接判断mBinding.userName.text == null永远不会成立。
    //应这样判断：mBinding.userName.text.toString().isEmpty()
    private fun registerToLoginFragment() {
        mBinding.registerButton.setOnClickListener {
            if (mBinding.userName.text?.isNotEmpty() == false || mBinding.userPassword.text?.isNotEmpty() == false || mBinding.userCheckPassword.text?.isNotEmpty() == false) {
                Toast.makeText(requireContext(), "请完善注册信息", Toast.LENGTH_SHORT).show()
            }
            if (mBinding.userPassword.text?.isNotEmpty() == true && mBinding.userCheckPassword.text?.isNotEmpty() == true && mBinding.userPassword.text.toString() != mBinding.userCheckPassword.text.toString()) {
                Toast.makeText(requireContext(), "两次密码不相同", Toast.LENGTH_SHORT).show()
            }
            if (mBinding.userName.text?.isNotEmpty() == true && mBinding.userPassword.text?.isNotEmpty() == true && mBinding.userCheckPassword.text?.isNotEmpty() == true) {
                if (mBinding.userPassword.text.toString() != mBinding.userCheckPassword.text.toString()) {
                    Toast.makeText(requireContext(), "两次密码不相同", Toast.LENGTH_SHORT).show()
                } else {
                    registerUser() //注册用户 添加用户信息到Room中 并重新跳转到登录界面
                    findNavController().navigate(R.id.action_registerFragment_to_loginFragment)
                    Toast.makeText(requireContext(), "注册成功 请重新登录", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    private fun registerUser() {
        //获取用户此时输入的注册用户信息 并插入到Room中
        userName = mBinding.userName.text.toString()
        userPassword = mBinding.userPassword.text.toString()
        userViewModel.insertUser(User(userName,userPassword))
    }

}